package com.ruoyi.xiaoshouchuku.service.impl;

import com.ruoyi.cangku.domain.Cangku;
import com.ruoyi.cangku.domain.Kucunpandian;
import com.ruoyi.cangku.mapper.CangkuMapper;
import com.ruoyi.cangku.mapper.KucunpandianMapper;
import com.ruoyi.common.utils.OrderUtils;
import com.ruoyi.goods.domain.Shangpinxinxi;
import com.ruoyi.goods.mapper.ShangpinxinxiMapper;
import com.ruoyi.xiaoshouchuku.domain.SalesOrder;
import com.ruoyi.xiaoshouchuku.domain.XiaoshouChuku;
import com.ruoyi.xiaoshouchuku.dto.SalesOrderDto;
import com.ruoyi.xiaoshouchuku.mapper.SalesOrderMapper;
import com.ruoyi.xiaoshouchuku.mapper.XiaoshouChukuMapper;
import com.ruoyi.xiaoshouchuku.service.ISalesOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class SalesOrderServiceImpl implements ISalesOrderService {

    @Autowired
    private SalesOrderMapper salesOrderMapper;

    @Autowired
    private ShangpinxinxiMapper shangpinxinxiMapper;

    @Autowired
    private CangkuMapper cangkuMapper;

    @Autowired
    private KucunpandianMapper kucunpandianMapper;

    @Autowired
    private XiaoshouChukuMapper xiaoshouChukuMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSalesOrder(SalesOrderDto salesOrderDto) {

        String orderId= OrderUtils.createOrderSN();
        salesOrderDto.setOrderId(orderId);
        List<XiaoshouChuku> xiaoshouChukuList = salesOrderDto.getOrderGoodsList();

        List<XiaoshouChuku> calculationXiaoshouChukuList = new ArrayList<>();
        for(XiaoshouChuku item:xiaoshouChukuList) {
            Cangku cangku = cangkuMapper.selectCangkuById(item.getChukuCangkuId());
            Shangpinxinxi shangpin = shangpinxinxiMapper.selectShangpinxinxiById(item.getGoodId());
            Kucunpandian fitter = new Kucunpandian();
            fitter.setCangkuId(item.getChukuCangkuId());
            fitter.setGoodId(item.getGoodId());
            Kucunpandian kucun = kucunpandianMapper.selectKucunpandianByGoodIdAndCangkuId(fitter);

            if ( kucun!= null && kucun.getKuncun().compareTo(item.getShuliang()) > -1) {
                XiaoshouChuku xiaoshouChuku = new XiaoshouChuku();
                xiaoshouChuku.setChukuDanhao(orderId);
                xiaoshouChuku.setChukuDate(salesOrderDto.getOrderDate());
                xiaoshouChuku.setChukuJingshouren(salesOrderDto.getSalesPerson());
                xiaoshouChuku.setJine(shangpin.getShangpinLingshoujia().multiply(item.getShuliang()));
                xiaoshouChuku.setChukuCangku(cangku.getCangkuName());
                xiaoshouChuku.setGoodId(item.getGoodId());
                xiaoshouChuku.setGoodName(shangpin.getShangpinName());
                xiaoshouChuku.setGoodGuige(shangpin.getShangpinGuige());
                xiaoshouChuku.setGoodBianma(shangpin.getShangpinId());
                xiaoshouChuku.setShuliang(item.getShuliang());
                xiaoshouChuku.setChukuDanjia(shangpin.getShangpinLingshoujia());
                xiaoshouChuku.setChukuCangkuId(item.getChukuCangkuId());
                xiaoshouChuku.setBeizhu(salesOrderDto.getBeizhu());
                BigDecimal oldKucun = kucun.getKuncun();
                kucun.setKuncun(oldKucun.subtract(item.getShuliang()));
                calculationXiaoshouChukuList.add(xiaoshouChuku);
                xiaoshouChukuMapper.insertXiaoshouChuku(xiaoshouChuku);
                kucunpandianMapper.updateKucunpandian(kucun);
            } else {
                throw new RuntimeException("仓库:" + cangku.getCangkuName() + ",商品:" + shangpin.getShangpinName() + "库存不足!");
            }
        }
        BigDecimal sumOrderPrice = calculationXiaoshouChukuList.stream().map(XiaoshouChuku::getJine ).reduce(BigDecimal.ZERO, BigDecimal::add);
        salesOrderDto.setOrderPrice(sumOrderPrice);
        return salesOrderMapper.insertSalesOrder(salesOrderDto);
    }


    @Override
    public List<SalesOrder> selectOrderList(SalesOrder salesOrder){

       return  salesOrderMapper.selectOrderList(salesOrder);
    }

}
